package com.xiao.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiao.train.business.domain.Train;
import com.xiao.train.business.domain.TrainCarriage;
import com.xiao.train.business.domain.TrainExample;
import com.xiao.train.business.mapper.TrainMapper;
import com.xiao.train.business.req.TrainQueryReq;
import com.xiao.train.business.req.TrainSaveReq;
import com.xiao.train.business.resp.TrainQueryResp;
import com.xiao.train.common.exception.BusinessException;
import com.xiao.train.common.exception.BusinessExceptionEnum;
import com.xiao.train.common.resp.PageResp;
import com.xiao.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class TrainService {

    @Resource
    TrainMapper trainMapper;

    public void save(TrainSaveReq req) {
        DateTime now = DateTime.now();
        Train train = BeanUtil.copyProperties(req, Train.class);
        if (ObjectUtil.isNull(train.getId())) {
            Train tmp = selectByUnique(req.getCode());
            if (ObjectUtil.isNotNull(tmp)) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
            }
            train.setId(SnowUtil.getId());
            train.setCreateTime(now);
            train.setUpdateTime(now);
            trainMapper.insert(train);
        } else {
            train.setUpdateTime(now);
            trainMapper.updateByPrimaryKey(train);
        }
    }
    public PageResp<TrainQueryResp> queryList(TrainQueryReq req) {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        TrainExample.Criteria criteria = trainExample.createCriteria();
        log.info("查询页码: {}", req.getPage());
        log.info("每页条数: {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Train> trains = trainMapper.selectByExample(trainExample);

        PageInfo<Train> pageInfo = new PageInfo<>(trains);
        log.info("总行数: {}", pageInfo.getTotal());
        log.info("总页数: {}", pageInfo.getPages());

        PageResp<TrainQueryResp> pageResp = new PageResp<>();
        List<TrainQueryResp> list = BeanUtil.copyToList(trains, TrainQueryResp.class);
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Transactional
    public List<TrainQueryResp> queryAll() {
        List<Train> trains = selectAll();
        return BeanUtil.copyToList(trains, TrainQueryResp.class);
    }

    public List<Train> selectAll() {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        return trainMapper.selectByExample(trainExample);
    }

    public void delete(Long id) {
        trainMapper.deleteByPrimaryKey(id);
    }
    private Train selectByUnique(String code) {
        TrainExample trainExample = new TrainExample();
        TrainExample.Criteria criteria = trainExample.createCriteria();
        criteria.andCodeEqualTo(code);
        List<Train> trains = trainMapper.selectByExample(trainExample);
        return trains.size() > 0 ? trains.get(0) : null;
    }
}
